var keyMap = {};
//定义一个对象,将bom中的地址参数存下来,java中的map集合
var categoryId = location.search.substring(1);
categoryId.split('&').forEach(function (element) {
    var km = element.split('=');
    keyMap[km[0]] = km[1];
}, this);

var num = 1;
var strss;
var categoryname;
$(function () {
    
    strss = $('.product_info').clone(true);
    $('.product_info').remove();
    $.ajax({
        url: 'http://127.0.0.1:3000/api/getcategorybyid',
        datatype: 'JSONP',
        data: {
            categoryid: keyMap.categoryid
        },
        success: function (data) {
            console.log(data);
            data = data.result;
            console.log(data);
            $('.product_Title').text(data[0].category);
            categoryname = data[0].category;
        }
    });


    show(num);
    $('.nextpage').on('click', function () {
    var pag =$(".select_page").attr("pagesize");
        num++;
        console.log(pag);
        if (num > pag) {
            num = pag;
        }
        // $(".currentpage").text(num);
        show(num);
    });
    $('.lastpage').on('click', function () {
        num--;
        if (num < 1) {
            num = 1;
        }
        // $(".currentpage").text(num);
        show(num);
    });

    // select改变,页面改变!
    $(".select_page").on('change', function () {
        num = $(this).val();
        // $(".currentpage").text(num);
        show(num);
    });

});


function show(page) {
    $.ajax({
        url: 'http://127.0.0.1:3000/api/getproductlist',
        datatype: 'JSONP',
        data: {
            categoryid: keyMap.categoryid,
            pageid: page
        },
        success: function (data) {
            //计算页数,向上取整!
            var maxPage = Math.ceil(data.totalCount / data.pagesize);
            // 生成下拉列表.
            $(".select_page").empty();
            $(".select_page").attr("pagesize",maxPage);
            for (var ind = 1; ind < maxPage + 1; ind++) {
                $(".select_page").append($("<option/>", {
                    value: ind,
                    text: ind
                }));
            }
            $(".select_page").val(num);
            $('.maxpage').text(maxPage);
            $('.productlist_item').empty();
            data = data.result;
            var len = data.length;
            console.log(data);
            console.log(categoryname);
            for (var i = 0; i < len; i++) {
                var aa = strss.clone(true);
                var idata = data[i];
                aa.find('.product_img').html(idata.productImg);
                aa.find('.toparticular').attr('href','particulars.html?productid='+idata.productId+encodeURI('&category='+categoryname));
                aa.find('.toparticular').text(idata.productName);
                aa.find('.producttext price').html(idata.productPrice);
                aa.find('.producttext .reportprice').html(idata.productQuote);
                aa.find('.producttext .discuss').html(idata.productCom);
                $('.productlist_item').append(aa);
            }
        }
    });
}